package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-03-29
 * Time:16:34
 */
public class LoginPage extends Utils {
    public static String url="http://127.0.0.1:8080/login.html";
    public LoginPage() {
        super(url);
    }
    //查看是否有登录页面
    public void LoginPageRight() throws InterruptedException {
        //通过查看页面元素是否存在来检查页面加载是否成功
        driver.findElement(By.cssSelector("body > div.login-container > div"));
        //页面上是否出现登录文字
        driver.findElement(By.cssSelector("body > div.login-container > div > h3"));
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));

    }
    //登录成功
    public void LoginSuc() throws InterruptedException {
        //清除原有的用户名和密码
        driver.navigate().refresh();
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(3000);
        //出现登录成功弹窗，点击确定进入列表页
        Alert alert=driver.switchTo().alert();
        alert.accept();
        Thread.sleep(3000);
        //检查点击确定之后是否登陆成功，进入会话列表页
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend"));
        Thread.sleep(3000);
    }
    //登录失败
    //1.用户名，密码错误
    public void LoginFail() throws InterruptedException {
        //清除原有的用户名和密码
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan1");
        driver.findElement(By.cssSelector("#password")).sendKeys("1234");
        driver.findElement(By.cssSelector("#submit")).click();
        //等待弹窗
        Thread.sleep(3000);
        //出现登录失败弹窗，点击确定按钮重新登陆
        Alert alert=driver.switchTo().alert();
        String except="登录失败!";
        String res=driver.switchTo().alert().getText();
        assert res.equals(except);
        alert.accept();
        Thread.sleep(3000);
        driver.navigate().refresh();
        Thread.sleep(3000);
    }
    //2.用户名和密码为空
    public void LoginFailNull() throws InterruptedException {
        //清除原有的用户名和密码
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#submit")).click();
        //等待弹窗
        Thread.sleep(3000);
        //出现登录失败弹窗，点击确定按钮重新登陆
        Alert alert=driver.switchTo().alert();
        String except="当前输入的用户名或者密码为空!";
        String res=driver.switchTo().alert().getText();
        assert res.equals(except);
        alert.accept();
        Thread.sleep(3000);
        driver.navigate().refresh();
        Thread.sleep(3000);
    }
}
